package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalServiceOfferConsts;
import com.iwhalecloud.bss.kite.cucc.common.util.DateUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.LocalOrderDelayAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

@Service
@AttrHandler(LocalOrderDelayAttrHandler.class)
public class LocalOrderDelayAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Resource
    private OperateInstAttrService operateInstAttrService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        // 不是延期业务，过滤
        if (LocalServiceOfferConsts.SERVICE_OFFER_DELAY.equals(data.getServiceOfferId())) {
            // 体验终止时间
            KiteAttr endTimeAttr = inst.getAttr("200004294");
            if (Objects.nonNull(endTimeAttr)) {
                try {
                    Date expTime = DateUtils.parseStrToDate(endTimeAttr.getOriginValue(), DateUtils.DATE_FORMAT);
                    if ("100002891".equals(attr.getValue())) {
                        expTime = DateUtils.addDay(expTime, 15);
                    } else if ("100006802".equals(attr.getValue())) {
                        expTime = DateUtils.addDay(expTime, 30);
                    } else if ("100006803".equals(attr.getValue())) {
                        expTime = DateUtils.addDay(expTime, 45);
                    } else if ("100006804".equals(attr.getValue())) {
                        expTime = DateUtils.addDay(expTime, 60);
                    } else if ("100006805".equals(attr.getValue())) {
                        expTime = DateUtils.addDay(expTime, 90);
                    }
                    String newExpTime = DateUtils.formatDate(expTime, DateUtils.DATE_FORMAT);
                    operateInstAttrService.setAttrValue(inst, "200004294", newExpTime, data.getSceneInstId(), null);
                } catch (Exception e) {
                    ErrorConsts.BUSI_COMMON_ERROR.throwOut("延期业务联动处理异常：" + e.getMessage());
                }
            }
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
}
